package cn.edu.neu.studentmsg_java.controller.workbench;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.Appointment;
import cn.edu.neu.studentmsg_java.service.AppointmentService;
import cn.edu.neu.studentmsg_java.service.CourseService;
import cn.edu.neu.studentmsg_java.service.TeacherService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/workbench/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    //获取课程名称数据
    @RequestMapping("/getCourseData")
    public R getCourseData() {
        //查询课程名称信息
        List<String> courseNameList = courseService.findCourseName();
        if (ObjectUtils.isNotEmpty(courseNameList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(courseNameList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //获取教师名称数据
    @RequestMapping("/getTeacherData")
    public R getTeacherData() {
        //查询教师名称信息
        List<String> teacherNameList = teacherService.findTeacherName();
        if (ObjectUtils.isNotEmpty(teacherNameList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(teacherNameList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过各种条件去查询教师排课信息
    @RequestMapping("/findAppointmentCPart")
    public R findAppointmentCPart(Appointment appointment) {
        //动态查询教师排课信息
        List<Appointment> appointmentList = appointmentService.findAppointmentCPart(appointment);
        //判断查询出的教师排课信息不为空
        if (ObjectUtils.isNotEmpty(appointmentList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(appointmentList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过各种条件去查询课程被哪些老师进行授课的信息
    @RequestMapping("/findAppointmentTPart")
    public R findAppointmentTPart(Appointment appointment) {
        //动态查询课程被哪些老师进行授课的信息
        List<Appointment> appointmentList = appointmentService.findAppointmentTPart(appointment);
        //判断查询出的结果不为空
        if (ObjectUtils.isNotEmpty(appointmentList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(appointmentList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //查询教师名称和课程名称
    @RequestMapping("/findTeacherNameAndCourseName")
    public R findTeacherNameAndCourseName(Appointment appointment) {
        //判断课程id不为空或教师id不为空
        if (appointment.getCourse_id() != null || appointment.getTeacher_id() != null) {
            //定义一个list集合，用于存放教师名称和课程名称
            List<Object> list = new ArrayList<>();
            //将教师名称通过教师id查询出来，并存放到集合当中
            list.add(teacherService.findTeacherNameByTeacherId(appointment.getTeacher_id()));
            //将课程名称通过课程id查询出来，并存放到集合当中
            list.add(courseService.findCourseNameByCourseId(appointment.getCourse_id()));
            //判断该集合是否为空
            if (ObjectUtils.isNotEmpty(list)) {
                return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(list).build();
            }
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过教师名称和课程名称删除它们之间的关联（删除老师授课信息）
    @RequestMapping("/removeAppointmentById")
    public R removeAppointmentByTeacherIdAndCourseId(Appointment appointment) {
        //通过教师名称和课程名称删除它们之间的关系（删除授课表中的信息）
        int result = appointmentService.removeAppointmentByTeacherIdAndCourseId(appointment);
        //判断删除结果是否大于0，大于0则成功删除
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_DELETE_ERROR.getCode()).msg(State.DB_DELETE_ERROR.getMsg()).build();
    }

    //通过教师id和课程名称添加它们之间的关联（添加老师授课信息）
    @RequestMapping("/addApprointmentByTIdAndCName")
    public R addApprointmentByTIdAndCName(@RequestBody Appointment appointment) {
        //通过教师id和课程名称添加它们之间的关联（添加授课表中的信息）
        int result = appointmentService.addApprointmentByTIdAndCName(appointment);
        //如果结果大于0，则证明添加成功
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }

    //通过教师名称和课程id去添加他们之间的关联（添加教师授课信息）
    @RequestMapping("/addApprointmentByTNameAndCId")
    public R addApprointmentByTNameAndCId(@RequestBody Appointment appointment) {
        //通过教师名称和课程id去添加他们之间的关联（添加授课表中的信息）
        int result = appointmentService.addApprointmentByTNameAndCId(appointment);
        //如果结果大于0，证明添加成功
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }
}
